//
//  VistorLoginView.swift
//  MAGweibo
//
//  Created by MagicaL on 15/7/29.
//  Copyright © 2015年 MagicaL. All rights reserved.
//

import UIKit

/// 访客视图协议
protocol VistorLoginViewDelegate : NSObjectProtocol {
    
    /// 将要登录
    func vistorLoginViewWillLogin()
    /// 将要注册
    func vistorLoginViewWillRegister()
    
}
/// 访客视图
class VistorLoginView: UIView {

    /// 定义代理 一定要用 weak
    weak var delegate : VistorLoginViewDelegate?
    
    func clickReigsterBtn() {
    
        delegate?.vistorLoginViewWillRegister()
    }
    
    func clickLoginBtn() {
    
        delegate?.vistorLoginViewWillLogin()
    }
    
    // MARK: - 设置视图信息
    func setupViewInfo(isHome: Bool, imageName: String, message: String) {
        
        messageLabel.text = message
        iconView.image = UIImage(named: imageName)
        homeIconView.hidden = !isHome
        isHome ? startAnimation() : sendSubviewToBack(maskIconView)
    }
    /// 动画
    private func startAnimation() {
        
        let animate = CABasicAnimation(keyPath: "transform.rotation")
        animate.toValue = 2 * M_PI
        animate.repeatCount = MAXFLOAT
        animate.duration = 20.0
        
        animate.removedOnCompletion = false
        
        iconView.layer.addAnimation(animate, forKey: nil)
    
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        // 通过纯代码方式创建SB时 不执行此方法
        // fatalError("init(coder:) has not been implemented")
        setupUI()
    }

    private func setupUI() {
        
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        // MARK: - 自动布局
        // 转圈图标
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -60))
        // 小房子
        homeIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        // 描述信息
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 15))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        // 注册按钮 
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        // 登录按钮
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: 0))
        // 遮罩视图
        maskIconView.translatesAutoresizingMaskIntoConstraints = false

        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview":maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-(-50)-[regBtn]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview":maskIconView,"regBtn":registerBtn]))
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1.0)
        
    }
    
    // MARK: - lazy
    /// 转圈图片
    private lazy var iconView :UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
        return imageView
    }()
    /// 首页小房子
    private lazy var homeIconView : UIImageView = {
    
        let iamgeView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
        return iamgeView
    }()
    /// 描述信息
    private lazy var messageLabel : UILabel = {
        
        let label = UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.textAlignment = NSTextAlignment.Center
        label.numberOfLines = 0
//        label.text = "关注一些人，回这里看看有什么惊喜"
        label.sizeToFit()
        return label
    }()
    /// 注册按钮
    private lazy var registerBtn : UIButton = {
        
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "clickReigsterBtn", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    /// 登录按钮
    private lazy var loginBtn : UIButton = {
        
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        btn.addTarget(self, action: "clickLoginBtn", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    /// 遮罩视图
    private lazy var maskIconView : UIImageView = {
        
        let view = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
        return view
    }()
    
}
